/*
* 朱冯博
* */
var sliderView = {
    sliderData: null,
    pageUrl: "",
    userType: 1,
    hasFindTopLevel: false,
    init: function(sliderData, pageUrl, userType) {
        this.pageUrl = pageUrl;
        this.sliderData = sliderData;
        this.userType = parseInt(userType);
    },
    getChildMenuItem: function(title, sliderData) {//递归获取子树
        var isHashOneLevel = true;
        if(!sliderData) {
            return [];
        }

        for(var index = 0; index < sliderData.length; index++) {
            if(sliderData[index].title && sliderData[index].title != title) {
                this.getChildMenuItem(title, sliderData[index].data);
            } else if(sliderData[index].title && sliderData[index].title == title) {
                isHashOneLevel = false;
                return sliderData[index].data;
            }
        }
        if(isHashOneLevel) {
            return sliderData;
        }
    },
    findTopLevelMenuItem: function(sliderData) {
        var isFind = false;
        if(!sliderData) {
            return [];
        }
        for(var index = 0; index < sliderData.length; index++) {
            if(sliderData[index].title) {
                isFind = this.findTopLevelMenuItem(sliderData[index].data);
                if(isFind) {
                    break;
                }
            } else if(sliderData[index].url && this.pageUrl.indexOf(sliderData[index].url) > -1) {
                return true;
            }
        }
        return isFind;
    },
    isUrlEqual: function(pageUrl, childMenuUrl) {
        var pageUrls = pageUrl.split("?");
        var childMenuUrls = childMenuUrl.split("?");
        var pagePathName = pageUrls[0].substring(pageUrls[0].lastIndexOf("/") + 1, pageUrls[0].length);
        var menuPathName = childMenuUrls[0].substring(childMenuUrls[0].lastIndexOf("/") + 1, childMenuUrls[0].length);
        if(pageUrls.length == 1 && childMenuUrls.length == 1 && pagePathName == menuPathName) {
            return true;
        } else if(pagePathName == menuPathName && pageUrls[1] && childMenuUrls[1] &&  pageUrls[1] == childMenuUrls[1]) {
            return true;
        } else if(pagePathName == menuPathName && (pageUrls[1] && !childMenuUrls[1])) {
            return true;
        } else {
            return false;
        }
    },
    renderChildMenu: function(parentNode, childMenuData) {
        if(!childMenuData) {
            return;
        }
        if(childMenuData && !childMenuData.length) {
            return;
        }
        var ulEle = $("<ul class='child-menu-box'></ul>");
        parentNode.append(ulEle);
        for(var index = 0; index < childMenuData.length; index++) {
            var liEle;
            var liHeight = 0;
            if(childMenuData[index].title) {
                liEle = $('<li class="child-title"><div class="menu-title">' + childMenuData[index].title + '</div></li>');
            } else {
                var activeCls = (childMenuData[index].url && (this.isUrlEqual(this.pageUrl, childMenuData[index].url)) || this.pageUrl.indexOf(childMenuData[index].relate) > -1) ? "active" : "";
                var href = childMenuData[index].url && this.isUrlEqual(this.pageUrl, childMenuData[index].url) ? childMenuData[index].url : childMenuData[index].url;
                if(childMenuData[index].name == "直接下单" && this.userType == 2) {
                    var link = "<a href=" + "/trdOrdersList" + ">" + childMenuData[index].name + "</a>";
                } else if((childMenuData[index].name == "运费试算" || childMenuData[index].name == "运单查询") && this.userType == 2) {
                    continue;
                }  else {
                    var link = "<a href=" + href + ">" + childMenuData[index].name + "</a>";
                }
                liEle = $("<li class='" + activeCls + "'><div class='border-active'></div>" + link + "</li>");
            }
            ulEle.append(liEle);
            if(!liEle.hasClass("child-title")) {
                liHeight = liEle.height();
                liEle.outerHeight(0);
                liEle.animate({height: liHeight + "px"});
            }
        }
        $(".menu-title", ulEle).click();
    },
    bindEvent: function() {
        var self = this;
        $(".slider-box").delegate(".menu-title", "click", function() {
            $(this).toggleClass("opening");
            var topNode = $(this).closest(".item-title");
            var topNodeIndex = parseInt(topNode.attr("data-index"));
            var parentNode = $(this).closest("li");
            if(parentNode.attr("hasChild")) {
                var ulEle = $("ul:eq(0)", parentNode);
                ulEle.animate({height:0}, function() {
                    ulEle.remove();
                });
                parentNode.removeAttr("hasChild");
                return;
            } else {
                parentNode.attr("hasChild", "true");
            }
            var title = $(this).html();
            var childMenuData = self.getChildMenuItem(title, self.sliderData[topNodeIndex].data);
            self.renderChildMenu(parentNode, childMenuData);
        });
    },
    openAllSlider: function() {
        //$(".menu-title").click();
    },
    openCurrentSlider: function() {
        var menuTitleEles = $(".menu-title");
        for(var index = 0; index < menuTitleEles.length; index++) {
            var hasFindTopLevel = this.findTopLevelMenuItem(this.sliderData[index].data, false);
            if(hasFindTopLevel) {
                $(menuTitleEles[index]).click();
                break;
            }
        }
    }
}